using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.UserDomain;
//using ShivamEMR.Web.Areas.User.Models;
using ShivamEMR.Model.DataInterfaces.PersonDataInterf;
using ShivamEMR.Model.DataInterfaces.UsersDataInterf;
using ShivamEMR.Model.DataInterfaces.UIDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;
using PagedList;

namespace ShivamEMR.Web.Areas.User.Controllers
{   
    public class UserController : Controller
    {
		private readonly ISexRepository sexRepository;
		private readonly IPersonPictureRepository personpictureRepository;
		private readonly IPersonAccountRepository personaccountRepository;
		private readonly IPersonStatusRepository personstatusRepository;
		private readonly IUserRoleRepository userroleRepository;
		//private readonly IMenuGroupRepository menugroupRepository;
		private readonly IUserRepository userRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public UserController() 
        {
            sexRepository = Registry.RepositoryFactory.GetSexRepository();
            personpictureRepository = Registry.RepositoryFactory.GetPersonPictureRepository();
            personaccountRepository = Registry.RepositoryFactory.GetPersonAccountRepository();
            personstatusRepository = Registry.RepositoryFactory.GetPersonStatusRepository();
            userroleRepository = Registry.RepositoryFactory.GetUserRoleRepository();
            //menugroupRepository = Registry.RepositoryFactory.GetMenuGroupRepository();
            userRepository = Registry.RepositoryFactory.GetUserRepository();

        }

        public UserController(ISexRepository sexRepository, IPersonPictureRepository personpictureRepository, IPersonAccountRepository personaccountRepository, IPersonStatusRepository personstatusRepository, IUserRoleRepository userroleRepository, IUserRepository userRepository)
        {
			this.sexRepository = sexRepository;
			this.personpictureRepository = personpictureRepository;
			this.personaccountRepository = personaccountRepository;
			this.personstatusRepository = personstatusRepository;
			this.userroleRepository = userroleRepository;
		//	this.menugroupRepository = menugroupRepository;
			this.userRepository = userRepository;
        }

        //
        // GET: /User/
        //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index(string searchString, int? page)
        {
            var users = userRepository.All;
            if (!String.IsNullOrEmpty(searchString))
            {
                users = users.Where(u=> u.Person.FirstName.ToUpper().Contains(searchString.ToUpper())
                                   || u.Person.MiddleName.ToUpper().Contains(searchString.ToUpper())
                                   || u.Person.LastName.ToUpper().Contains(searchString.ToUpper())
                    || u.Name.ToUpper().Contains(searchString.ToUpper()) 
                    || u.Role.Role.ToUpper().Contains(searchString.ToUpper()   ));
            }
            var plist = users.ToList<ShivamEMR.Model.Domain.UserDomain.User>();
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(plist.ToPagedList(pageNumber, pageSize)); 
           // return View(userRepository.AllIncluding(user => user.Sex, user => user.Picture, user => user.PersonStatus, user => user.Addresses, user => user.Contacts, user => user.CitizenShips, user => user.Informations, user => user.PersonRoles, user => user.Groups, user => user.Documents, user => user.Assignments, user => user.InteractionsWith, user => user.Role, user => user.DefaultDashboard, user => user.Rights, user => user.Log, user => user.Messages));
           // return View(userRepository.All);
        }

        //
        // GET: /User/Details/5
         //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(System.Guid id)
        {
            return View(userRepository.Find(id));
        }
            

        

        //
        // GET: /User/Create
          //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Create()
        {
            ViewBag.PossibleEmployees = Registry.RepositoryFactory.GetEmployeeRepository().All;
            ViewBag.PossibleRoles = userroleRepository.All;              
            var user = new ShivamEMR.Model.Domain.UserDomain.User();
            return View(user);  
        } 

        //
        // POST: /User/Create
          //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(ShivamEMR.Model.Domain.UserDomain.User user)
        {
            if (ModelState.IsValid)
            {
                userRepository.Create(user);
                userRepository.Save();
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.PossibleSexes = sexRepository.All;
                ViewBag.PossiblePictures = personpictureRepository.All;
                ViewBag.PossiblePersonAccounts = personaccountRepository.All;
                ViewBag.PossiblePersonStatus = personstatusRepository.All;
                ViewBag.PossibleRoles = userroleRepository.All;
           //     ViewBag.PossibleDefaultDashboards = menugroupRepository.All;

                ViewBag.PossibleEmployees = Registry.RepositoryFactory.GetEmployeeRepository().All;
                ViewBag.PossibleRoles = userroleRepository.All;              
                var usr = new ShivamEMR.Model.Domain.UserDomain.User();
                return View(usr);
            }
        }
        
        //
        // GET: /User/Edit/5
   //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleSexes = sexRepository.All;
			ViewBag.PossiblePictures = personpictureRepository.All;
			ViewBag.PossiblePersonAccounts = personaccountRepository.All;
			ViewBag.PossiblePersonStatus = personstatusRepository.All;
            ViewBag.PossibleEmployees = Registry.RepositoryFactory.GetEmployeeRepository().All;
            ViewBag.PossibleRoles = userroleRepository.All;                         
             return View(userRepository.Find(id));
        }

        //
        // POST: /User/Edit/5
          //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(ShivamEMR.Model.Domain.UserDomain.User user)
        {
            if (ModelState.IsValid) {
                //userRepository.InsertOrUpdate(user);
                userRepository.Update(user);
                userRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSexes = sexRepository.All;
				ViewBag.PossiblePictures = personpictureRepository.All;
				ViewBag.PossiblePersonAccounts = personaccountRepository.All;
				ViewBag.PossiblePersonStatus = personstatusRepository.All;
                ViewBag.PossibleEmployees = Registry.RepositoryFactory.GetEmployeeRepository().All;
                ViewBag.PossibleRoles = userroleRepository.All;              
				return View();
			}
        }

        //
        // GET: /User/Delete/5
   //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(System.Guid id)
        {
            return View(userRepository.Find(id));
        }

        //
        // POST: /User/Delete/5
          //[CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            //userRepository.Delete(id);
            var usr = userroleRepository.Find(id);
            userroleRepository.Delete(usr);
            userRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

